const app = getApp()
const cnConvert = require('../../utils/changefont.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
      userInfo: {},
      noBind:true,
      userName:'',
      jname:'',
      stime:'',
      etime:'',
      idDaili:false,
      leagueList:[],//加盟商列表
      partnerList:[],//合作商数据列表 
      contractorList:[],//承包商合作列表
      pageText:{
        'lj':'了解咖缘吧更多信息',
        'jm':'我的加盟商',
        'hz':'我的合作商',
        'cb':'我的承包商',
        'kf':'我的咖啡机',
        'gy':'关于我们',
        'jc':'退出',
        'jgt':'警告',
        'jg':'解除绑定后，将无法看到您的数据，需要重新绑定或者登录'

      }

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (wx.getStorageSync('userDo').font == 1) { //切换繁体

      var textObj = this.data.pageText
      for (var item in textObj) {
        textObj[item] = cnConvert.traditionalized(textObj[item])
      }
      this.setData({
        pageText: textObj
      })
     
    }
  },


  getLeague:function(stype){
   
    return new Promise((relsove,reject) => {
      wx.request({
        url: app.globalData.api + 'getAgentListByAgentId',
        data: {
          limit: 300,
          offset: 0,
          parent: wx.getStorageSync('userDo').id,
          type: stype,
          tfont: wx.getStorageSync('userDo').font
        },
        success: function (res) {
          if(res.data){
            relsove(res.data)
          }else{
            reject(res.data)
          }
        }

      })
    })
  },

  jm:function(){
    wx.navigateTo({
      url: '/pages/jiameng/jiameng?stype=2',
    })
  },
  hz: function () {
    wx.navigateTo({
      url: '/pages/jiameng/jiameng?stype=3',
    })
  },
  cb: function () {
    wx.navigateTo({
      url: '/pages/jiameng/jiameng?stype=4',
    })
  },

  initData:function(){
    var that = this;
    this.getLeague(2).then(function(res){
      that.setData({
        leagueList:res
      })
    })
    this.getLeague(3).then(function (res) {
      that.setData({
        partnerList: res
      })
    })
    this.getLeague(4).then(function (res) {
      that.setData({
        contractorList: res
      })
    })
  },

  onShow:function(){
      if (wx.getStorageSync('userDo')) {
          var user = wx.getStorageSync('userDo');
          var n = ''
          if(user.type == 1){
            n ='代理商'
          }else if(user.type == 2){
            n = '加盟商'
          }else if(user.type == 3){
            n = '合作商'
          }else{
            n = '承包商'
          }


         
          this.setData({
              noBind: false,
              userName: wx.getStorageSync('userDo').name,
              stime: wx.getStorageSync('userDo').startTime.substring(0, 10),
              etime: wx.getStorageSync('userDo').endTime.substring(0, 10),
              idDaili: wx.getStorageSync('userDo').type == 1 ? true : false,
              jname:n
          })
          this.initData();
      } else {
          this.setData({
              noBind: true,
          })
      }
     
  },
  myCoffee:function(){
      wx.navigateTo({
          url: '/pages/myCoffee/myCoffee',
      })
  },
  goBind: function () {
      wx.navigateTo({
          url: '/pages/bindAccount/bindAccount',
      })
  },
  about:function(){
      wx.navigateTo({
          url: '/pages/about/about',
      })
  },
  jBind:function(){
     var that  = this;


     wx.showModal({
       title: that.data.pageText.jgt,
       content: that.data.pageText.jg,
         success:function(res){
             if (res.confirm){
                 that.relieveBindAgent();
             }
         }
     })
  },
  relieveBindAgent: function () {
      var that = this;
      var user = wx.getStorageSync('userDo');
      if (wx.getStorageSync('isLogin')){
        wx.removeStorageSync('userDo')

        wx.showToast({
          title: "退出登录成功！"
        })
        wx.setStorageSync('isLogin', false)
        setTimeout(function () {
          wx.switchTab({
            url: '/pages/home/home',
          })
        }, 1000)
        return 
      }
      wx.request({
          url: app.globalData.api + "relieveBindAgent",
          data: {
              id: user.id
          },
          success: function (res) {

              if (res.data.code == 500) {
                  wx.showToast({
                      title: res.data.msg,
                      icon: 'none'
                  })
              } else if (res.data.code == 0) {
                  wx.removeStorageSync('userDo')

                  wx.showToast({
                      title: "操作成功！"
                  })
                  setTimeout(function () {
                      wx.switchTab({
                          url: '/pages/home/home',
                      })
                  }, 1000)
              }
          }

      })
  }
 
})